/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __IAUDIO_H__
#define __IAUDIO_H__

#pragma once
#include <string>
#include <map>
#include "IModule.h"
#include "ITransformable.h"
#include "Config.h"

namespace audio {

enum eStates_ISound {
	ISOUND_PLAYING,
	ISOUND_STOPPED
};

typedef unsigned int SfxID;
typedef unsigned int FileID;

class ISound;
class ISoundBuffer;
class IJukeBox;

/**
 * \ingroup CoreEngine
 *
 * \date 03-28-2007
 *
 * \author dennis
 *
 * \todo 
 *
 * \bug 
 *
 */
class DLL_EXPORT IAudio
	: public IModule
{
public:
	/**
	*	Clean up. Cleans all ISounds and IStreamingSounds.
	*/
	virtual void Destroy()=0;

	/**
	*	Init the audio engine.
	*/
	virtual bool init(bool b3D)=0;
	
	/**
	*	The update loop.
	*/
	virtual void update(double dt)=0;

	/**
	*	Create an ISound by name. Sounds are not loaded twice.
	*	\param fileName Name of soundfile.
	*	\return ISound The created ISound
	*/
	virtual ISound* createISound(const std::string &fileName)=0;

	/**
	*	Create an IStreamingSound by name. Sounds are not loaded twice.
	*	\param fileName Name of soundfile.
	*	\return ISound The created ISound
	*/
	virtual ISound* createIStreamingSound(const std::string &fileName)=0;

	/**
	*	De-register ISound from audio-engine.
	*	Is called from destructor of ISound!
	*/
	virtual void destroyISound(ISound* _pSound, float _fadeTime=0)=0;

	/**
	*	Create and destroy an IJukeBox
	*/
	virtual IJukeBox* createIJukeBox()=0;
	virtual void destroyIJukeBox(IJukeBox* _jb)=0;

	// functions for getting/ setting the global sfx, music, and speech volumes:
	// setting them also writes the values in the config file
	float getSfxVolume(bool _ignoreMute = true);
	void  setSfxVolume( float vol);

	float getMusicVolume();
	void  setMusicVolume( float vol);

	float getSpeechVolume();
	void  setSpeechVolume(float vol);

	virtual void muteSFX(bool _val);

	/**
	* Make a short fade out before stopping a sound.
	*/
	virtual void stopISound(ISound* _pSound, float _fFadeTime = 0.1f)=0;
	
protected:

	/**
	*	Create an ISoundBuffer from a Soundfile
	*	Supported formats (hopefully):
	*	-wav
	*	-aiff
	*	-ogg	
	*/
	virtual ISoundBuffer* createSoundBufferFromFile(const std::string &fileName)=0;

	/**
	*	Function that erases all ISoundBuffers
	*/
	void destroyAllBuffers();

	IAudio(void);
	virtual ~IAudio(void);

	std::map<std::string, ISoundBuffer*> m_mISoundBuffer; ///<Map of all opened SoundFiles
	
	float m_fSfxVolume;		///< The global Sfx volume
	float m_fMusicVolume;	///< The music Sfx volume
	float m_fSpeechVolume;	///< The speech Sfx volume
	
	bool m_bSFXMuted;
};

} // namespace


/**
 *  Initialize the audio engine
 */
DLL_EXPORT
bool coreInitAudio(bool b3D);

/**
 *  Destroy the audio engine
 */
DLL_EXPORT
bool coreDestroyAudio();

/**
 *  Returns a reference to the current audio engine object. 
 */
DLL_EXPORT 
audio::IAudio* coreGetAudio();


#endif // __IAUDIO_H__
